package com.yang.leather.common.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {
	private static String top = "{";
	private static String top_ = "}"; 
	
	public static String getJsonByList(List dList){
		String data = "";
		if(dList != null){
			JSONArray ja = JSONArray.fromObject(dList);
			data = ja.toString();
		}
		return data;
	}
	
	/***
	 *  具有对象标识json数据
	 *  @param params
	 *  @return
	 */
	public static String getObjectJsonData(HashMap params){
		StringBuffer data = new StringBuffer();
		int i = 0;
		if(params != null){
			data.append(top);
			Iterator it = params.entrySet().iterator();
			while(it.hasNext()){
				Entry element = (Entry)it.next(); 
				String key = (String)element.getKey();
				List dList = (ArrayList)element.getValue();
				if(i > 0)data.append(",");
				data.append("\"" + key + "\"" + ":");
				data.append(getJsonByList(dList));	
				i++;
			}
			data.append(top_);
		}
		return data.toString();
	}
	
	/***
	 *  针对ext表json格式数据
	 *  @param dList
	 *  @return
	 */
	public static String getExtGridJsonData(List dList){
		StringBuffer data = new StringBuffer();
		if(dList != null){
			data.append("{\"totalCount\":" + dList.size() + ", \"Body\":");
            data.append(getJsonByList(dList));
			data.append("}");
		}
		return data.toString();
	}
	
	public static String getGridJsonData(int rowNumber, List dList){
		StringBuffer data = new StringBuffer();
		if(dList != null){
			data.append("{\"totalCount\":" + rowNumber + ", \"Body\":");
            data.append(getJsonByList(dList));
			data.append("}");
		}
		return data.toString();
	}
	
	
	public static String getDefineJsonData(String firstParam,String listParam,String firstValue,List dList){
		StringBuffer data = new StringBuffer();
		if(dList != null){
			data.append("{\""+firstParam+"\":" + firstValue + ", \""+ listParam +"\":");
            data.append(getJsonByList(dList));
			data.append("}");
		}
		return data.toString();
	}
	
	/***
	 *  基础json数据
	 *  @param dList
	 *  @return
	 */
	public static String getBasetJsonData(List dList){
			StringBuffer data = new StringBuffer();
			if(dList != null){
				JSONArray ja = JSONArray.fromObject(dList);
				data.append(ja.toString());
			}
			return data.toString();
		
	}
	
	/***
	 *  基础json数据
	 *  @param dMap
	 *  @return
	 */
	public static String getBasetJsonData(Map dMap){
			StringBuffer data = new StringBuffer();
			if(dMap != null){
				JSONObject ja = JSONObject.fromObject(dMap);
				data.append(ja.toString());
			}
			return data.toString();
		
	}
	
	/**
	 * 用简单的json数据转为Map
	 * 
	 * @param json
	 *            {one:1,tow:2,.....}
	 * @return
	 */
	public static Map getMapFromSimpleJson(String json) {
		JSONObject jso = JSONObject.fromObject(json);
		Map<Object, Object> map = new HashMap<Object, Object>();

		for (Iterator iterator = jso.keys(); iterator.hasNext();) {
			String element = (String) iterator.next();
			map.put(element, jso.get(element));
		}
		return map;
	}
	/**
	 * 将json转换为clazz的List，json格式为{'任意':[{},{}..]}
	 * T类的属性与json属性命名一样则赋值，否则忽略
	 * @param <T>
	 * @param obj json字符串
	 * @param clazz 
	 * @return
	 */
	public static <T> List<T> getJson2List(String obj, Class<T> clazz) {		
		JSONObject jsonObject = JSONObject.fromObject(obj);
		List<T> list = new ArrayList<T>();
		for (Iterator iter = jsonObject.keys(); iter.hasNext();) {

			String key = (String) iter.next();
			JSONArray array = jsonObject.getJSONArray(key);
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = (JSONObject) array.get(i);				
				T t =null;
				try {
					t = clazz.newInstance();
					for (Iterator it = object.keys(); it.hasNext();) {
						String keyi = (String) it.next();
						String valuei = (String) object.get(keyi);
						Field f;
						try {
							f = clazz.getDeclaredField(keyi);
							f.setAccessible(true);
							f.set(t, valuei);
						} catch (Exception e) {
							
						} 
					}
					list.add(t);
				} catch (InstantiationException e1) {
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					e1.printStackTrace();
				}
				
    

			}
		}

		return list;
	}  
	public static void main(String[] args){
		Map map = new HashMap();
		map.put("a", "tes1");
		map.put("b", "12");
		List list = new ArrayList();
		list.add("aa");
		list.add("bb");
		System.out.println(getJsonByList(list));
	}
	

}
